package com.cn.longdistancebusstation.scenic.order;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.callback.OnRecyclerViewItemClickListener;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.ListResult;
import com.cn.longdistancebusstation.model.ScenicOrderList;
import com.cn.longdistancebusstation.util.ToolUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by mucheng on 2018/3/30.
 */

public class ScenicOrderListActivity extends BaseActivity implements OnRecyclerViewItemClickListener {
    private RecyclerView mScenicOrderRecyclerView;
    private ScenicOrderAdapter mScenicOrderAdapter;
    private List<ScenicOrderList> mSelectTicketOrderList=new ArrayList<>();
    private String num;
    private String realNum;
    private String ticketNum;
    private String ticketNumber;
    private double mDouble;
    private String strStart="http";
    private String scenicPath="http://www.12301.cc/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scenic_order_list);
        getTitleTextView().setText("门票订单");
        initView();
        getScenicOrderList();

    }

    private void initView() {
        mScenicOrderRecyclerView=(RecyclerView)findViewById(R.id.scenic_order_list);
        mScenicOrderAdapter=new ScenicOrderAdapter();
        mScenicOrderRecyclerView.setAdapter(mScenicOrderAdapter);
        mScenicOrderRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mScenicOrderRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mScenicOrderAdapter.setmOnRecyclerViewItemClickListener(this);

    }
    private void getScenicOrderList() {
        mSelectTicketOrderList.clear();
        showHud();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService service=retrofit.create(HttpService.class);
        Call<ListResult<ScenicOrderList>> call=service.getScenicOrderList(GlobalVariable.getUserID());
        call.enqueue(new Callback<ListResult<ScenicOrderList>>() {
            @Override
            public void onResponse(Call<ListResult<ScenicOrderList>> call, Response<ListResult<ScenicOrderList>> response) {
                hideHud();
                ListResult result=response.body();
                if(result!=null){
                    if(result.getHead().getSuccess()){
                        Gson gson=new Gson();
                        String strBody=gson.toJson(result.getBody().getData());
                        Log.d("数据",strBody);
                        Type type=new TypeToken<List<ScenicOrderList>>(){}.getType();
                        mSelectTicketOrderList =gson.fromJson(strBody, type);
                        Log.d("景点数量", String.valueOf(mSelectTicketOrderList.size()));
                        mScenicOrderAdapter.notifyDataSetChanged();

                    }

                }



            }

            @Override
            public void onFailure(Call<ListResult<ScenicOrderList>> call, Throwable t) {
                hideHud();
                Toast.makeText(ScenicOrderListActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onClick(int position) {
        ScenicOrderList scenicOrderList=mSelectTicketOrderList.get(position);
        Intent in=new Intent(ScenicOrderListActivity.this,ScenicOrderDetailsActivity.class);
        in.putExtra("orderId",scenicOrderList.getOrderId());
        startActivity(in);
    }

    @Override
    public void onClickInnerCtrl(int position) {

    }

    @Override
    public void onClickCoupon(int position) {

    }

    @Override
    public void onClickCheckBox(int position) {

    }

    private class ScenicOrderAdapter extends RecyclerView.Adapter<ScenicOrderListViewholder>{
        private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;
        public void setmOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener) {
            this.mOnRecyclerViewItemClickListener = mOnRecyclerViewItemClickListener;
        }

        @Override
        public ScenicOrderListViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(ScenicOrderListActivity.this);
            View view = layoutInflater.inflate(R.layout.scenic_order_recycle_item, parent, false);
            return new ScenicOrderListViewholder(view);
        }

        @Override
        public void onBindViewHolder(ScenicOrderListViewholder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnRecyclerViewItemClickListener.onClick(position);

                }
            });
            ScenicOrderList scenicOrderList=mSelectTicketOrderList.get(position);
            holder.bindData(scenicOrderList);


        }

        @Override
        public int getItemCount() {
            return mSelectTicketOrderList.size();
        }

    }

    private class ScenicOrderListViewholder extends RecyclerView.ViewHolder{
        private ImageView mScenicPic;
        private TextView mOrderName;
        private TextView mOrderStatus;
        private TextView mOrderNum;
        private TextView mAmountPrice;
        public ScenicOrderListViewholder(View itemView) {
            super(itemView);
            mScenicPic=(ImageView)itemView.findViewById(R.id.imageView_scenic);
            mOrderName=(TextView)itemView.findViewById(R.id.textView_scenic_name);
            mOrderStatus=(TextView)itemView.findViewById(R.id.textView_order_state);
            mOrderNum=(TextView)itemView.findViewById(R.id.textView_amount);
            mAmountPrice=(TextView)itemView.findViewById(R.id.textView_order_amount_price);
        }

        public void bindData(ScenicOrderList scenicOrderList) {
            if(scenicOrderList.getImagePathe().startsWith(strStart)){
                Picasso.with(ScenicOrderListActivity.this).load(scenicOrderList.getImagePathe()).placeholder(R.mipmap.pre_loading)
                        .error(R.mipmap.pre_loading).noFade().into(mScenicPic);
            }else{
                Picasso.with(ScenicOrderListActivity.this).load(scenicPath+scenicOrderList.getImagePathe()).placeholder(R.mipmap.pre_loading)
                        .error(R.mipmap.pre_loading).noFade().into(mScenicPic);
                Log.d("图片地址",scenicPath+scenicOrderList.getImagePathe());
            }
            mDouble= Double.parseDouble(scenicOrderList.getNum());
            ticketNum= BigDecimal.valueOf(mDouble).stripTrailingZeros().toPlainString();
            mOrderName.setText(scenicOrderList.getPltitle()+"("+scenicOrderList.getTtitle()+")"+ticketNum+"张");
            String status=scenicOrderList.getStatus();
            if("0".equals(status)){
                mOrderStatus.setText("待付款");
            }else if("1".equals(status)){
                mOrderStatus.setText("待使用");
            }else if("2".equals(status)){
                mOrderStatus.setText("已取消");
            }else if("3".equals(status)){
                mOrderStatus.setText("已使用");
            }else if("4".equals(status)){
                mOrderStatus.setText("已退票");
            }else if("5".equals(status)){
                mOrderStatus.setText("订单异常");
            }else if("6".equals(status)){
                mOrderStatus.setText("退票待审核");
            }else{
                mOrderStatus.setText("退票失败");
            }
            mOrderNum.setText(ticketNum);
            mAmountPrice.setText(ToolUtil.fen2Yuan(scenicOrderList.getTotalPrice())+"元");
            num=scenicOrderList.getNum();
            realNum=scenicOrderList.getRealNum();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getScenicOrderList();

    }
}
